﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using KVB.Common;

namespace Customer_Profile.eCard.DBDao
{
    public class CpartyDao
    {
        public static string AddCparty(string cparty_name,string entity, string phone, string id_type,
            string id_no,string sex,string birthday,string email,string zipcode,string address )  //添加一个客户
        {
            SqlConnection conn = null;
            try
            {
                DB.Connection(ref conn);
                SqlParameter[] parameters ={ new SqlParameter("@cparty_name", SqlDbType.VarChar,250),
                                             new SqlParameter("@phone",SqlDbType.VarChar,250),
                                             new SqlParameter("@id_type",SqlDbType.VarChar,250),
                                             new SqlParameter("@id_no",SqlDbType.VarChar,250 ),
                                             new SqlParameter("@sex",SqlDbType.NVarChar,250),
                                             new SqlParameter("@birthday",SqlDbType.VarChar,250),
                                             new SqlParameter("@email",SqlDbType.VarChar,50),
                                             new SqlParameter("@zipcode",SqlDbType.VarChar,250),
                                             new SqlParameter("@address",SqlDbType.NVarChar,250),
                                             new SqlParameter("@entity",SqlDbType.VarChar,250),
                                             new SqlParameter("@cparty_no",SqlDbType.VarChar,250)};
                parameters[10].Direction = ParameterDirection.Output;

                parameters[0].Value = cparty_name;
                parameters[1].Value = phone;
                parameters[2].Value = id_type;
                parameters[3].Value = id_no;
                parameters[4].Value = sex;
                parameters[5].Value = birthday;
                parameters[6].Value = email;
                parameters[7].Value = zipcode;
                parameters[8].Value = address;
                parameters[9].Value = entity;
                DB.ExecuteNonQuery(conn, CommandType.StoredProcedure, "eCard_cparty_insert", parameters);
                return parameters[10].Value.ToString();
            }
            catch (Exception e)
            {
                KVB.Common.Log.WriteDefaultLog("CpartyDao.AddCparty", e.Message);
                return "";
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
        }

        public static DataSet SearchCpartybyCpartyID(string cparty_id)  //根据IDNO获取客户信息
        {
            SqlConnection conn = null;
            DataSet ds = null;
            try
            {
                DB.Connection(ref conn);
                SqlParameter[] parameters = { new SqlParameter("@cparty_id", SqlDbType.VarChar, 50) };
                parameters[0].Value = cparty_id;
                ds = DB.ExecuteDataset(conn, CommandType.StoredProcedure, "eCard_getcparty_bycpartyid", parameters);
            }
            catch (Exception e)
            {
                KVB.Common.Log.WriteDefaultLog("CpartyDao.SearchCpartybyIDno", e.Message);
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
            return ds;
        }

        public static void EditCparty(string cparty_id,string cparty_name, string phone, string id_type,
            string id_no, string sex, string birthday, string email, string zipcode, string address)  //修改一个客户
        {
            SqlConnection conn = null;
            try
            {
                DB.Connection(ref conn);
                SqlParameter[] parameters ={ 
                                             new SqlParameter("@cparty_id",SqlDbType.VarChar,250),
                                             new SqlParameter("@cparty_name", SqlDbType.VarChar,250),
                                             new SqlParameter("@phone",SqlDbType.VarChar,250),
                                             new SqlParameter("@id_type",SqlDbType.VarChar,250),
                                             new SqlParameter("@id_no",SqlDbType.VarChar,250 ),
                                             new SqlParameter("@sex",SqlDbType.NVarChar,250),
                                             new SqlParameter("@birthday",SqlDbType.VarChar,250),
                                             new SqlParameter("@email",SqlDbType.VarChar,50),
                                             new SqlParameter("@zipcode",SqlDbType.VarChar,250),
                                             new SqlParameter("@address",SqlDbType.NVarChar,250)
                                            };
                parameters[0].Value = cparty_id;
                parameters[1].Value = cparty_name;
                parameters[2].Value = phone;
                parameters[3].Value = id_type;
                parameters[4].Value = id_no;
                parameters[5].Value = sex;
                parameters[6].Value = birthday;
                parameters[7].Value = email;
                parameters[8].Value = zipcode;
                parameters[9].Value = address;

                DB.ExecuteNonQuery(conn, CommandType.StoredProcedure, "eCard_cparty_Update", parameters);
            }
            catch (Exception ex)
            {
                KVB.Common.Log.WriteDefaultLog("CpartyDao.EditCparty", ex.Message);
                throw ex;
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
        }

        public static DataSet SearchCpartybyIDno(string idno)  //根据IDNO获取客户信息
        {
            SqlConnection conn = null;
            DataSet ds = null;
            try
            {
                DB.Connection(ref conn);
                SqlParameter[] parameters ={new SqlParameter("@id_no",SqlDbType.VarChar,50)};
                parameters[0].Value = idno;
                ds =DB.ExecuteDataset(conn, CommandType.StoredProcedure, "eCard_getcparty_byidno", parameters);
            }
            catch (Exception e)
            {
                KVB.Common.Log.WriteDefaultLog("CpartyDao.SearchCpartybyIDno", e.Message);
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
            return ds;
        }

       

        public static DataSet getCpartyBank(string strcpartyid)
        {
            string strSQL = "SELECT * FROM cparty_bankaccount where cparty_id ='"+ strcpartyid +"'";
            SqlConnection conn = null;
            DataSet ds = null;
            try
            {
                DB.Connection(ref conn);
                ds = DB.ExecuteDataset(conn, CommandType.Text, strSQL);
            }
            catch (Exception e)
            {
                KVB.Common.Log.WriteDefaultLog("CpartyDao.getCpartyBank", e.Message);
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
            return ds;
        }

        public static DataSet addCpartyBank(string cpartyid,string account_name,string account_no,string bank_name,string bank_branch,string bank_subbranch,string strlocation ,string strcity)
        {
            string strSQL = "insert into cparty_bankaccount(cparty_id,account_name,account_no,bank_name,bank_branch,bank_sub_branch,location,city) values ('"+ cpartyid +"','"+ account_name +"','" + account_no + "','"+ bank_name +"','" + bank_branch + "','"+ bank_subbranch +"','"+ strlocation +"','"+ strcity +"')";
            SqlConnection conn = null;
            DataSet ds = null;
            try
            {
                DB.Connection(ref conn);
                ds = DB.ExecuteDataset(conn, CommandType.Text, strSQL);
            }
            catch (Exception e)
            {
                KVB.Common.Log.WriteDefaultLog("CpartyDao.getCpartyBank", e.Message);
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
            return ds;
        }

        public static DataSet updateCpartyBank(string bankaccount_id, string account_name, string account_no, string bank_name, string bank_branch, string bank_subbranch, string strlocation, string strcity,string in_use)
        {
            string strSQL = "update cparty_bankaccount set account_name='" + account_name + "',account_no='" + account_no + "',bank_name ='" + bank_name + "',bank_branch='" + bank_branch + "',bank_sub_branch='" + bank_subbranch + "',location='" + strlocation + "',city='" + strcity + "',in_use='" +in_use  + "' where Bankaccount_id='" + bankaccount_id + "'";
            SqlConnection conn = null;
            DataSet ds = null;
            try
            {
                DB.Connection(ref conn);
                ds = DB.ExecuteDataset(conn, CommandType.Text, strSQL);
            }
            catch (Exception e)
            {
                KVB.Common.Log.WriteDefaultLog("CpartyDao.getCpartyBank", e.Message);
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
            return ds;
        }

        public static DataSet deleteCpartyBank(string bankaccount_id)
        {
            string strSQL = "update cparty_bankaccount set in_use='N' where Bankaccount_id='" + bankaccount_id + "'";
            SqlConnection conn = null;
            DataSet ds = null;
            try
            {
                DB.Connection(ref conn);
                ds = DB.ExecuteDataset(conn, CommandType.Text, strSQL);
            }
            catch (Exception e)
            {
                KVB.Common.Log.WriteDefaultLog("CpartyDao.deleteCpartyBank", e.Message);
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
            return ds;
        }

        public static DataSet getCpartyList(string strWhere)
        {
            string strSQL = "SELECT cparty_id ,cparty_name ,entity  ,phone ,id_type ,id_no ,sex ,birthday ,email ,zipcode ,address ,in_use	FROM cparty where in_use='Y' ";
            strSQL += strWhere;
            //KVB.Common.Log.WriteDefaultLog("strSQL:", strSQL);
            SqlConnection conn = null;
            DataSet ds = null;
            try
            {
                DB.Connection(ref conn);
                //SqlParameter[] parameters = { new SqlParameter("@strWhere", SqlDbType.VarChar, 50) };
                //parameters[0].Value = strWhere;
                ds = DB.ExecuteDataset(conn, CommandType.Text, strSQL);
            }
            catch (Exception e)
            {
                KVB.Common.Log.WriteDefaultLog("CpartyDao.getCpartyList", e.Message);
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
            return ds;
        }
    }
}